package controllers

import (
	"github.com/astaxie/beego"
	"autoCRUD/models"
	"encoding/json"
	"autoCRUD/service"
)

type SpiderItemController struct {
	beego.Controller
}

// @Description SpiderItem
// @router /spiderItem/add [post]
func (this *SpiderItemController) AddSpiderItem() {
	spiderItem := &models.SpiderItem{}
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, item); err != nil {
		this.Data["json"] = map[string]interface{}{"is_success": false, "msg": err.Error()}
		this.ServeJSON()
		return
	}

	err := service.AddSpiderItem(item)
	if err != nil{
		this.Data["json"] = map[string]interface{}{"is_success": false, "msg": err.Error()}
		this.ServeJSON()
		return
	}
	this.Data["json"] = map[string]interface{}{"is_success": true, "msg": "添加SpiderItem成功", "content": spiderItem}
	this.ServeJSON()
	return
}

// @Description 修改SpiderItem
// @router /spiderItem/update [post]
func (this *SpiderItemController) UpdateSpiderItem() {
	spiderItem := &models.SpiderItem{}
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, spiderItem); err != nil {
		this.Data["json"] = map[string]interface{}{"is_success": false, "msg": err.Error()}
		this.ServeJSON()
		return
	}

	err := service.UpdateSpiderItem(spiderItem)
	if err != nil{
		this.Data["json"] = map[string]interface{}{"is_success": false, "msg": err.Error()}
		this.ServeJSON()
		return
	}
	this.Data["json"] = map[string]interface{}{"is_success": true, "msg": "修改SpiderItem成功", "content": demo}
	this.ServeJSON()
	return
}

// @Description 删除SpiderItem
// @router /spiderItem/delete [post]
func (this *SpiderItemController) DeleteSpiderItem() {
	type ID struct {
		Id int `json:"id"`
	}
	id := &ID{}
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, id); err != nil {
		this.Data["json"] = map[string]interface{}{"is_success": false, "msg": err.Error()}
		this.ServeJSON()
		return
	}

	err := service.DeleteSpiderItem(id.Id)
	if err != nil{
		this.Data["json"] = map[string]interface{}{"is_success": false, "msg": err.Error()}
		this.ServeJSON()
		return
	}
	this.Data["json"] = map[string]interface{}{"is_success": true, "msg": "删除SpiderItem成功"}
	this.ServeJSON()
	return
}


// @Description 查询SpiderItem
// @router /spiderItems [get]
func (this *SpiderItemController) ListSpiderItem(){

	spiderItems,err := service.ListSpiderItem()
	if err != nil {
		this.Data["json"] = map[string]interface{}{"is_success": false, "msg": err.Error()}
		this.ServeJSON()
		return
	}
	this.Data["json"] = map[string]interface{}{"is_success": true, "msg": "获取采集项列表成功", "content": spiderItem}
	this.ServeJSON()
	return
}

// @Description 根据ID获取SpiderItem
// @router /spiderItem [get]
func (this *SpiderItemController) GetSpiderItemById(){
	id, _ := this.GetInt("id",-1)
	if id == -1{
		this.Data["json"] = map[string]interface{}{"is_success": false, "msg": "必须提供ID参数"}
		this.ServeJSON()
		return
	}
	spiderItem,err := service.SpiderItemById(id)
	if err != nil {
		this.Data["json"] = map[string]interface{}{"is_success": false, "msg": err.Error()}
		this.ServeJSON()
		return
	}
	this.Data["json"] = map[string]interface{}{"is_success": true, "msg": "获取SpiderItem成功", "content": spiderItem}
	this.ServeJSON()
	return
}
